import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiUtilBase;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

public class ToJson extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        PsiFile psiFile = PsiUtilBase.getPsiFileInEditor(editor, project);

        StringBuilder jsonText = new StringBuilder("{\n");
        for (PsiElement psiElement : psiFile.getChildren()) {
            if (psiElement instanceof PsiClass) {
                PsiClass psiClass = (PsiClass) psiElement;
                PsiField[] allFields = psiClass.getAllFields();
                for (int i = 0; i < allFields.length; i++) {
                    PsiField field = allFields[i];
                    if (field.hasModifierProperty(PsiModifier.STATIC) || field.hasModifierProperty(PsiModifier.FINAL)) {
                        continue;
                    }
                    String fieldName = field.getName();
                    String fieldType = field.getType().getCanonicalText();
                    PsiDocComment docComment = field.getDocComment();
                    String desc = "";
                    if (null != docComment) {
                        desc = (docComment.getText().replaceAll("(/\\*\\*\\s*)|(\\*\\s*)|(\\*/)|(\\s*/)", "")).trim();
                    }
                    String splitText = ",\n";
                    if (i == allFields.length - 1) {
                        splitText = "\n";
                    }
                    if ("java.lang.Integer".equals(fieldType)) {
                        jsonText.append(String.format("\t\"%s\":%s %s", fieldName, 0, splitText));
                    } else if ("java.lang.Double".equals(fieldType)) {
                        jsonText.append(String.format("\t\"%s\":%s %s", fieldName, 0.0, splitText));
                    } else if ("java.lang.Boolean".equals(fieldType)) {
                        jsonText.append(String.format("\t\"%s\":%s %s", fieldName, true, splitText));
                    } else {
                        jsonText.append(String.format("\t\"%s\":%s %s", fieldName, String.format("\"%s\"",desc), splitText));
                    }
                }
            }
        }
        jsonText.append("}");
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(jsonText.toString());
        clip.setContents(tText, null);
        Messages.showInfoMessage("Copied json successfully", "tip");
    }
}
